#include <unistd.h>
#include <errno.h>
#include "utils.h"

extern int slt_errno;

int slt_utils_read(int fd, char *buf, int len) {
    int n = 0, nread;
    for (;;) {
        nread = read(fd, buf + n, len - n);
        if (nread < 0) {
            if (errno == EINTR) {
                continue;
            } else {
                break;
            }
        } else if (nread == 0) {
            return 0;
        } else {
            n += nread;
            if (n == len) {
                slt_errno = EAGAIN;
                break;
            } else {
                continue;
            }
        }
    }
    return n;
}
